package com.zh.controller;

import com.github.pagehelper.PageInfo;
import com.zh.controller.result.Code;
import com.zh.controller.result.Result;
import com.zh.controller.system.exception.BusinessException;
import com.zh.domain.User;
import com.zh.service.userService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/users")
public class userController {

    @Autowired
    private userService service;

    @PostMapping
    public Result add(User user) {
        boolean flag = service.add(user);
        return new Result(flag ? Code.ADD_OK : Code.ADD_ERR);
    }

    @DeleteMapping("/{uuid}")
    public Result delete(@PathVariable Integer uuid) {
        boolean flag = service.delete(uuid);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR);
    }

    @PutMapping
    public Result Update(User user) {
        boolean flag = service.update(user);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR);
    }

    @GetMapping("/{uuid}")
    public Result getById(@PathVariable Integer uuid) {
        User flag = service.getById(uuid);
        if(flag == null){throw new BusinessException("查询的ID不存在",Code.SELECT_ERR);}
        return new Result((null != flag ? Code.SELECT_OK : Code.SELECT_ERR), flag);
    }

    @GetMapping("/{page}/{size}")
    public Result getAll(@PathVariable int page, @PathVariable int size) {
        PageInfo<User> all = service.getAll(page, size);
        List<User> flag = all.getList();
        return new Result((null != flag ? Code.SELECT_OK : Code.SELECT_ERR), flag);
    }

    @PostMapping ("/login")
    public Result login(String userName,String password) {
        User flag = service.login(userName, password);
        return new Result((null != flag ? Code.SELECT_OK : Code.SELECT_ERR), flag);
    }

}
